/*复选框*/
var goodlist = document.querySelector(".main_content");
var opt = goodlist.getElementsByClassName('options'); //全部的options类
var act = document.getElementsByClassName("active"); //选中后添加，让其background变红
var QX_opt = document.querySelector(".QX_options");
//var pp=goodlist.getElementsByClassName("options"); //总菜单main_content中的options类
var n = 0;
/*var arr = [];
var newarr = [];*/
var com_prices = document.getElementsByClassName("com_prices"); //购买物品的价格

for(let i = 0; i < opt.length; ++i) {
	//判断需要执行变化的函数
	if(opt[i].tagName === 'DIV') {
		//添加点击事件
		opt[i].addEventListener("click", function(e) {
			//捕获鼠标点击的目标元素
			if(e.target.nodeName == "I" && e.target.parentNode.nodeName == "DIV") {
				//动态改变状态
				opt[i].classList.toggle("active");
				/*if(opt[i].classList.contains("active") === "active") {
					arr.push(i);
					//去除重复元素
					arr.forEach(function(val) {
						if(newarr.indexOf(val) == -1) {
							newarr.push(val);
						}
					})
					console.log(newarr);
					console.log(typeof(newarr));
				} else {
					//再次选中数组元素时删除该元素
					for (let j=0;j<newarr.length;j++) {
						opt[i].onclick=function(){
							console.log("点击了第"+i+"个元素")
						}
					}
				}*/
				//在全选状态下，如果有某一选项被取消，则全选框也随之取消
				if(!opt[i].classList.contains("active")) {
					QX_opt.classList.remove("active");
				}
			}
		})
	}
}
//全选功能
QX_opt.addEventListener("click", function(e) {
	if(e.target.nodeName == "I" && e.target.parentNode.nodeName == "DIV") {
		if(QX_opt.classList.contains("active") == true) {
			//点击取消全选按钮
			QX_opt.classList.remove("active");
			//全选按钮被点击取消后对所有选项进行取消选择
			for(let i = 0; i < opt.length; ++i) {
				if(opt[i].tagName === 'DIV') {
					//取消选择，所以删除active
					opt[i].classList.remove("active");
				}
			}

		} else {
			//点击选择全选按钮
			QX_opt.classList.add("active");
			//全选按钮被点击后对所有选项进行点击选择
			for(let i = 0; i < opt.length; ++i) {
				if(opt[i].tagName === 'DIV') {
					//点击选择，所以增加active
					opt[i].classList.add("active");
				}
			}
		}
	}
})

/*领券弹窗*/
var LQbtn = document.querySelector(".LQbtn");
var MB_layer = document.querySelector(".MB_layer");
var LQ_layer = document.querySelector(".LQ_layer");
var LQ_cancel = document.querySelector(".LQ_cancel");
//领券
LQbtn.addEventListener("click", function() {
	MB_layer.style.display = "block";
	LQ_layer.style.display = "block";
})
//点击遮罩关闭弹窗
MB_layer.addEventListener("click", function() {
	MB_layer.style.display = "none";
	LQ_layer.style.display = "none";
})
//弹窗内部关闭
LQ_cancel.addEventListener("click", function() {
	MB_layer.style.display = "none";
	LQ_layer.style.display = "none";
})

//删除
var MctBj = document.querySelector(".main_content_title_BJ");
var Mbtm = document.querySelector(".main_bottom");
var Mlsc = document.getElementsByClassName("son3");

MctBj.addEventListener("click", function() {
	if(MctBj.innerText == '编辑') {
		Mbtm.style.display = 'none';
		MctBj.innerText = "完成";
		for(let i = 0; i < Mlsc.length; i++) {
			if(Mlsc[i].tagName === "DIV") {
				Mlsc[i].style.display = 'block';
				Mlsc[i].addEventListener("click", function(e) {
					if(e.target.className == "p_SC" && e.target.parentNode.nodeName == "DIV") {
						//捕获及判断成功
						goodlist.removeChild(e.target.parentNode.parentNode.parentNode.parentNode);
					}
				})
			}
		}

	} else {
		MctBj.innerText = "编辑";
		Mbtm.style.display = 'flex';
		for(let i = 0; i < Mlsc.length; i++) {
			if(Mlsc[i].tagName === "DIV") {
				Mlsc[i].style.display = 'none';
			}
		}
	}

});

//商品购买数量
var at1 = document.getElementsByClassName("at1"); //捕获减少购买物品数量按钮
var at2 = document.getElementsByClassName("at2"); //捕获增加购买物品数量按钮
var Number_pq = document.getElementsByClassName("purchase_quantity"); //当前购买物品的数量
for(let i = 0; i < at1.length; i++) {
	if(at1[i].tagName === "DIV") {
		at1[i].addEventListener("click", function(e) {
			if(e.target.nodeName == "I" && e.target.parentNode.nodeName == "DIV") {
				if(Number_pq[i].innerText == '0') {
					alert('不能再减了！');

				} else {
					Number_pq[i].innerText = Number_pq[i].innerText - 1;
					com_prices[i].innerText = parseFloat(Number_pq[i].innerText) * 299.00;
				}
			}
			console.log(e.target.parentNode.nodeName);
			console.log(typeof(Number_pq[i].innerText));
		})
	}
	if(at2[i].tagName === "DIV") {
		at2[i].addEventListener("click", function(e) {
			if(e.target.nodeName == "I" && e.target.parentNode.nodeName == "DIV") {
				Number_pq[i].innerText = Number(Number_pq[i].innerText) + 1;
				com_prices[i].innerText = Number(Number_pq[i].innerText) * 299.00;
			}
		})
	}
}
//统计总价 获取菜单中被选中的div